package com.ccrego.haiyi.quickysee;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ccrego.haiyi.R;
import com.ccrego.haiyi.db.PushMessageEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;

import java.text.SimpleDateFormat;

/**
 * Description:
 * Created by Andy Hong on 10/17/2017.
 */

public class QuickSeeAdapter extends RecyclerViewBaseAdapter<PushMessageEntity> {

    public QuickSeeAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new ArticleHolder(setAdapterViewItem(parent, R.layout.list_item_quick_see_article));
        }
        return new MessageHolder(setAdapterViewItem(parent, R.layout.list_item_quick_see_message));
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
        PushMessageEntity entity = getData().get(position);
        if (holder instanceof ArticleHolder) {
            ArticleHolder articleHolder = (ArticleHolder) holder;
            Glide.with(articleHolder.itemView.getContext())
                    .load(entity.getContent())
                    .into(articleHolder.ivPicture);
            articleHolder.tvDate.setText(formatSimpleDate(entity.getDate()));
            articleHolder.tvTitle.setText(entity.getTitle());
        } else if (holder instanceof MessageHolder) {
            MessageHolder articleHolder = (MessageHolder) holder;
            articleHolder.tvDate.setText(formatSimpleDate(entity.getDate()));
            articleHolder.tvMessage.setText(entity.getContent());
        }

    }

    private String formatSimpleDate(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(time);
    }

    @Override
    public int getItemViewType(int position) {
        return getData().get(position).getType();
    }

    private static class ArticleHolder extends RecyclerView.ViewHolder {
        private TextView tvDate;
        private ImageView ivPicture;
        private TextView tvTitle;

        public ArticleHolder(View itemView) {
            super(itemView);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            ivPicture = (ImageView) itemView.findViewById(R.id.iv_picture);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }

    }

    private static class MessageHolder extends RecyclerView.ViewHolder {
        private TextView tvDate;
        private TextView tvMessage;

        public MessageHolder(View itemView) {
            super(itemView);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            tvMessage = (TextView) itemView.findViewById(R.id.tv_message);
        }
    }
}
